/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "button.h"

static const char class_name [] = "EGUI Widget Button";

static int afc_easygui_internal_button_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_button ( DynamicClass * dyn );
static int afc_easygui_internal_button_create_button ( EasyGUIContext * data, char * text, char * iconname, char * action, DynamicClass * dc );
static int afc_easygui_internal_button_add_toolbar ( EasyGUIContext * data, char * text, char * iconname, char * action, DynamicClass * dc );
static int afc_easygui_internal_button_create_menu_item ( EasyGUIContext * context, char * text, char * iconname, char * action, DynamicClass * dc );

static void afc_easygui_internal_button_event_clicked ( GtkWidget * w, gpointer data );
static void afc_easygui_internal_button_event_activate ( GtkWidget * w, gpointer data );
static void afc_easygui_button_event_enter ( GtkWidget * w, gpointer data );
static void afc_easygui_button_event_leave ( GtkWidget * w, gpointer data );

static int afc_easygui_button_method_change_text ( DynamicClass * dyn );

/*
@node afc_easygui_internal_button_new_instance

@endnode
*/
DynamicClass * afc_easygui_internal_button_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) 
	{
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_button_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_button );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "change_text",   NULL,  afc_easygui_button_method_change_text );

	return ( dyn );
}

/*
@node afc_easygui_internal_button_del_instance

@endnode
*/
int afc_easygui_internal_button_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	//if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	//return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_button_get_template ( DynamicClass * dyn )
{
	dyn->result = "TEXT ACTION ICON SELICON TEXTPOS RELIEF SMALL/S";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_button ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	EasyGUIWidgetData * container;
	char * text;
	char * iconname;
	char * action;
	int afc_res = AFC_ERR_NO_ERROR;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "button" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, context );

	// We need to work on the current container
	container = context->curr_container;

	afc_easygui_widget_parse_default_params ( egui );

	text 	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "TEXT" );
	iconname = afc_cmd_parser_arg_get_by_name ( egui->parser, "ICON" );
	action	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "ACTION" );

	switch ( container->type )
	{
		case AFC_EASYGUI_CONTAINER_TYPE_TOOLBAR:
			afc_res = afc_easygui_internal_button_add_toolbar ( context, text, iconname, action, dc );
			break;

		case AFC_EASYGUI_CONTAINER_TYPE_MENU:		// Menu and OptMenu are almost the same...
		case AFC_EASYGUI_CONTAINER_TYPE_OPTMENU:
			afc_res = afc_easygui_internal_button_create_menu_item ( context, text, iconname, action, dc );
			break;

		default:
			afc_res = afc_easygui_internal_button_create_button ( context, text, iconname, action, dc );
			break;
	}

	return ( afc_res );
}

// Creates a "standard" button
static int afc_easygui_internal_button_create_button ( EasyGUIContext * context, char * text, char * iconname, char * action, DynamicClass * dc )
{
	int afc_res;
	GtkWidget * box = NULL;
	GtkWidget * w;
	char * text_pos;
	char * relief;
	char * sel_icon = NULL;

	relief = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "RELIEF" );

	w = gtk_button_new ();

	text_pos = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "TEXTPOS" );
	sel_icon = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "SELICON" );

	if ( sel_icon ) afc_easygui_preload_icon ( context->egui, sel_icon );

	box = afc_easygui_widget_create_label ( dc, text, iconname, afc_easygui_widget_get_horient ( context, text_pos ) );
	// add contents to button 
	gtk_container_add ( GTK_CONTAINER ( w ), box );

	gtk_button_set_relief ( GTK_BUTTON ( w ), afc_easygui_widget_get_relief ( context, relief ) );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) return ( afc_res );


	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "clicked", action, NULL, (GtkSignalFunc) afc_easygui_internal_button_event_clicked ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res );

	if ( sel_icon )
	{
		afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_STRING, "egui_icon_normal", iconname );
		afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_STRING, "egui_icon_selected", sel_icon );

		g_signal_connect ( G_OBJECT ( w ), "enter", G_CALLBACK ( afc_easygui_button_event_enter ), dc );
		g_signal_connect ( G_OBJECT ( w ), "leave", G_CALLBACK ( afc_easygui_button_event_leave ), dc );
	}

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_button_add_toolbar ( EasyGUIContext * context, char * text, char * iconname, char * action, DynamicClass * dc )
{
	GtkWidget * w;
	int size = GTK_ICON_SIZE_LARGE_TOOLBAR;
	short small;

	afc_easygui_widget_store_function ( context, dc, "clicked", action );

	small = ( short ) ( int ) afc_cmd_parser_arg_get_by_name ( context->egui->parser, "SMALL" );

	if ( small ) size = GTK_ICON_SIZE_SMALL_TOOLBAR;

	w = gtk_toolbar_insert_item ( 	GTK_TOOLBAR ( context->curr_container->gtk_handle ),
				       	text,
					context->egui->def_params->tip, context->egui->def_params->longtip,
					afc_easygui_load_icon ( context->egui, iconname, size ),
					GTK_SIGNAL_FUNC ( afc_easygui_internal_button_event_clicked ), // callback!	
					dc, // data
					-1 );

	afc_easygui_widget_register ( context, NULL, dc );

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_button_create_menu_item ( EasyGUIContext * context, char * text, char * iconname, char * action, DynamicClass * dc )
{
	int afc_res;
	GtkWidget * box = NULL;
	GtkWidget * w;
	char * text_pos;

	text_pos = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "TEXTPOS" );

	box = afc_easygui_widget_create_label ( dc, text, iconname, afc_easygui_widget_get_horient ( context, text_pos ) );
	w   = gtk_menu_item_new ();

	gtk_container_add ( GTK_CONTAINER ( w ), box );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) return ( afc_res );

	// We add the "activate" event only if the container is a MENU and not an OPTION MENU
	if ( context->curr_container->type == AFC_EASYGUI_CONTAINER_TYPE_MENU )
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "activate", action, NULL, (GtkSignalFunc) afc_easygui_internal_button_event_activate ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );
	}

	return ( AFC_ERR_NO_ERROR );
}

// METHODS
static int afc_easygui_button_method_change_text ( DynamicClass * dyn )
{
	char * txt;
	EasyGUIWidgetData * ewidget;

	txt = afc_array_master_first ( dyn->args );
	if ( txt == NULL ) return ( AFC_ERR_NO_ERROR );

	WIDGET_GET_EWIDGET ( dyn, ewidget );

	gtk_button_set_label ( GTK_BUTTON ( ewidget->gtk_handle ), txt );

	return ( AFC_ERR_NO_ERROR );
}

// EVENT FUNCTIONS
static void afc_easygui_internal_button_event_clicked ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUISignalFunc sig;

	WIDGET_CALL_CALLBACK ( dc, sig, "clicked" );
}

static void afc_easygui_internal_button_event_activate ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUISignalFunc sig;

	WIDGET_CALL_CALLBACK ( dc, sig, "activate" );
}

static void afc_easygui_button_event_enter ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	GtkWidget    * icon ;
	char * name;

	icon = WIDGET_GET_ICON ( dc );
	name = afc_dynamic_class_get_var ( dc, "egui_icon_selected" );

	afc_easygui_get_icon ( dc->info, icon, name, GTK_ICON_SIZE_BUTTON );
}

static void afc_easygui_button_event_leave ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	GtkWidget    * icon;
	char * name;

	icon = WIDGET_GET_ICON ( dc );
	name = afc_dynamic_class_get_var ( dc, "egui_icon_normal" );

	afc_easygui_get_icon ( dc->info, icon, name, GTK_ICON_SIZE_BUTTON );
}
